/**
*
*Account上添加Trigger，新建、修改、编辑客户信息时，根据客户等级以及相关业务机会下产品一级分类，对业务机会项目类型进行自动变更。
*1、意向到立项阶段由意向产品的计算，方案谈判及以后的阶段由报价产品/测算产品计算； 
*2、计算规则：单一产品线：单项目，两个及两个以上产品线：多项目；整体打包：三级以上医院5个及5个以上产品线，二级以上医院4及4个以上产品线； 
*     创建人：Leo
*     日期：2017-8-11
*
**/

public without sharing class AccountLevelHander {   


   public static void setOppProjectType(List<Account> newList,Map<Id, Account> oldMap) {

      String level = '';
      
      Set<Id> newqtidSet = new Set<Id>();
      Set<Id> accidSet = new Set<Id>();
      Set<Id> oppidSet = new Set<Id>();
      Set<Id> qpoppidSet = new Set<Id>();
     
      
      //获取更改后客户ID,客户等级
      for ( Account acc : newList ) {
         Account oldAcc = oldMap.get(acc.Id);
          if (oldAcc.Level__c != acc.Level__c) {
               accidSet.add(acc.Id);         
          }          
      }
       
       if (accidSet.size() == 0)  {
           system.debug('======================================------------------------');
           return;
       }
      
     
      //获取业务机会ID
      for ( Opportunity opp : [ SELECT Id, ProjectType__c FROM Opportunity WHERE AccountId IN: accidSet and StageName != '合同签订' and StageName != '失败' and Is_Stop__c != true ] ) {
         oppidSet.add(opp.Id);
      }     
      if(oppidSet.size() > 0)OppSetProjectTypeHandler.calcOppSetProjectType(oppidSet);
     /* //获取相关业务机会下仪器报价产品
      Map<Id, Set<String>> mcpMap = new Map<Id, Set<String>>();
      for ( QuoteProduct__c mcp : [ SELECT Id, Product__r.Category1__c,Quote__r.OpportunityId FROM QuoteProduct__c WHERE 
                    Quote__r.OpportunityId IN: oppidSet AND Product__r.Category0__c = '仪器' ] ) {
         Set<String> cat1Set = new Set<String>();
         Id mcpid = mcp.Quote__r.OpportunityId;
         if ( mcpMap.containsKey(mcpid)  == true ) {
            cat1Set = mcpMap.get(mcpid);
         }
         cat1Set.add(mcp.Product__r.Category1__c);
         mcpMap.put(mcpid, cat1Set);
      }
      //获取测算信息产品信息      
      for ( Measure_Calculate_Product__c mcp : [ SELECT Id, Product__r.Category1__c, Measure_Calculate__c, Measure_Calculate__r.Opportunity__c
            FROM Measure_Calculate_Product__c WHERE Measure_Calculate__r.Opportunity__c IN :oppidSet] ) {
         Set<String> cat1Set = new Set<String>();
         Id mcpid = mcp.Measure_Calculate__r.Opportunity__c;
         if ( mcpMap.containsKey(mcpid)  == true ) {
            cat1Set = mcpMap.get(mcpid);
         }
         cat1Set.add(mcp.Product__r.Category1__c);
         mcpMap.put(mcpid, cat1Set);
      }
      system.debug(mcpMap+'========================================================');

      
     
      
      //获取报价产品信息
      //获取业务机会ID(报价使用)
      for ( Opportunity opp : [ SELECT Id, ProjectType__c FROM Opportunity WHERE AccountId IN: accidSet AND RecordType.Name != '开发区域经销商业务机会'] ) {
         qpoppidSet.add(opp.Id);
      }
      //查询相关业务机会下最新的仪器报价
      for ( Quote qt : [ SELECT Id FROM Quote WHERE OpportunityID IN: qpoppidSet AND Type__c = '仪器报价' Order By LastModifiedDate DESC limit 1 ] ) {
         newqtidSet.add(qt.Id);
      }
      //查询相关业务机会下最新的试剂报价
      for ( Quote qt : [ SELECT Id FROM Quote WHERE OpportunityID IN: qpoppidSet AND Type__c = '试剂报价' Order By LastModifiedDate DESC limit 1 ] ) {
         newqtidSet.add(qt.Id);
      }
      Map<Id, Set<String>> qpMap = new Map<Id, Set<String>>(); 
      for ( QuoteProduct__c qp : [ SELECT Id, Product__r.Category1__c, Quote__r.OpportunityId FROM QuoteProduct__c WHERE Quote__c IN: newqtidSet ] ) {
         Set<String> cat1Set = new Set<String>();
         Id qpid = qp.Quote__r.OpportunityId;
         if ( qpMap.containsKey(qpid)  == true ) {
            cat1Set = qpMap.get(qpid);
         }
         cat1Set.add(qp.Product__r.Category1__c);
         qpMap.put(qpid, cat1Set);
      }
      system.debug(qpMap+'===========================================================');

      


      //获取意向产品信息
      Map<Id, Set<String>> ipMap = new Map<Id, Set<String>>(); 
      for ( IntentProduct__c ip : [SELECT Id, NewCategory1__c, Product__r.Category1__c, Opportunity__c FROM IntentProduct__c WHERE Opportunity__c in: oppidSet] ) {
         Set<String> cat1Set = new Set<String>();
         Id ipid = ip.Opportunity__c;
         if ( ipMap.containsKey(ipid)  == true ) {
            cat1Set = ipMap.get(ipid);
         }
         cat1Set.add(ip.Product__r.Category1__c);
         ipMap.put(ipid, cat1Set);
      }
      system.debug(ipMap);

      List<Opportunity> oppList = new List<Opportunity>();
      for ( Id oid : oppidSet ) {
         if ( mcpMap.get(oid) != null ) {
            Opportunity op = new Opportunity(Id = oid);
            //取得level和产品一级分类进行逻辑判断
            op.ProjectType__c  = IPQPMECPHandler.calcOppProjectType(level, mcpMap.get(oid));
            oppList.add(op);
         }
         else
         if ( qpMap.get(oid) != null ) {
            Opportunity op = new Opportunity(Id = oid);
            op.ProjectType__c  = IPQPMECPHandler.calcOppProjectType(level, qpMap.get(oid));
            oppList.add(op);
         }
         else
         if ( ipMap.get(oid) != null ) {
            Opportunity op = new Opportunity(Id = oid);
            op.ProjectType__c  = IPQPMECPHandler.calcOppProjectType(level, ipMap.get(oid));
            oppList.add(op);
         }
         else {
            Opportunity op = new Opportunity(Id = oid,ProjectType__c = ''); 
            oppList.add(op);
         }

      }
      update oppList;
      */
       
   }
}